package com.justech.eat.service.impl;

import com.justech.common.util.DateUtil;
import com.justech.eat.entity.AccMonitorLog;
import com.justech.eat.service.component.AccMonitorLogParser;

import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.List;

/**
 * @program: eat-service
 * @description:
 * @author: feifei.li
 * @create: 2020-05-26 14:11
 **/
public class AccMonitorLogParserImpl implements AccMonitorLogParser {
    private String date;
    private Date dateTime;
    private Date tomorrow;
    static final Long dayStamp = 1000L * 60L * 60L * 24L;
    private static Long hour = 1000L * 60 * 60;

    static final Long t1_start = hour * 10;
    static final Long t1_end = hour * 15;
    static final Long t2_start = hour * 16;
    static final Long t2_end = hour * 20;
    static final Long t3_start = hour * 23;
    static final Long t3_end = hour * 2;
    static final Long t4_start = hour * 4;
    static final Long t4_end = hour * 7;

    public AccMonitorLogParserImpl(String date) {
        this.date = date;
        this.dateTime = DateUtil.stringToDate(date);
        this.tomorrow = tomorrow(dateTime);
    }

    @Override
    public List<AccMonitorLog> parse(List<AccMonitorLog> logs) {
        for (AccMonitorLog log : logs) {
            parse(log);
        }
        return logs;
    }

    public String getDate() {
        return date;
    }

    public void setDate(String date) {
        this.date = date;
    }

    protected Date tomorrow(Date date) {
        Calendar calendar = new GregorianCalendar();
        calendar.setTime(date);
        calendar.add(calendar.DATE, 1);//把日期往后增加一天.整数往后推,负数往前移动
        return calendar.getTime();
    }

    protected Long getFullT1Start() {
        return this.dateTime.getTime() + t1_start;
    }

    protected Long getFullT1End() {
        return this.dateTime.getTime() + t1_end;
    }

    protected Long getFullT2Start() {
        return this.dateTime.getTime() + t2_start;
    }

    protected Long getFullT2End() {
        return this.dateTime.getTime() + t2_end;
    }

    protected Long getFullT3Start() {
        return this.dateTime.getTime() + t3_start;
    }

    protected Long getFullT3End() {
        return this.tomorrow.getTime() + t3_end;
    }

    protected Long getFullT4Start() {
        return this.tomorrow.getTime() + t4_start;
    }

    protected Long getFullT4End() {
        return this.tomorrow.getTime() + t4_end;
    }

    protected int doType(Date date) {
        if (date == null) return -1;
        long time = date.getTime();
        long eatPoint = time;
        if (eatPoint >= getFullT1Start() && eatPoint <= getFullT1End()) {
            return 1;
        } else if (eatPoint >= getFullT2Start() && eatPoint <= getFullT2End()) {
            return 2;
        } else if (eatPoint >= getFullT3Start() && eatPoint <= getFullT3End()) {
            return 3;
        } else if (eatPoint >= getFullT4Start() && eatPoint <= getFullT4End()) {
            return 4;
        }
        return 0;
    }

    /**
     * 主要目的是提取出数据中的餐别
     *
     * @param log
     * @return
     */
    @Override
    public AccMonitorLog parse(AccMonitorLog log) {
        log.setType(doType(log.getTime()));
        return log;
    }
}
